package cn.edu.cug.cs.gtl.apis.ui.tree;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;


public abstract class TreeSelectionListener implements javax.swing.event.TreeSelectionListener{
    protected AbstractApplication theApp=null;
    protected Tree theTree =null;
    protected TreeSelectionListener(Tree tree, AbstractApplication theApp){
        this.theApp = theApp;
        this.theTree =tree;
    }

    /**
     * Called whenever the value of the selection changes.
     *
     * @param e the event that characterizes the change.
     */
    @Override
    public void valueChanged(TreeSelectionEvent e) {
        TreePath treePath = e.getPath();
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
        this.theTree.setCurrentSelectedNode(treeNode);
        Object obj = this.theTree.getCurrentReferencedObject();
        if(obj!=null)
            objectChanged(obj);
    }

    protected abstract void objectChanged(Object obj);
}
